package com.zhanghe.study.design_pattern.composite_pattern;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合对象
 * @author zh
 * @date 2022/10/15 18:15
 */
public class Composite extends Component {
    private List<Component> childs = new ArrayList<>();
    @Override
    public void doSomething() {
        for(Component component : childs){
            component.doSomething();
        }
    }

    @Override
    public void addChild(Component child) {
        childs.add(child);
    }

    @Override
    public void removeChild(Component child) {
        if(childs.size() > 0){
            childs.remove(child);
        }
    }

    @Override
    public Component getChild(int index) {
        if(childs.size() > 0 && index <= childs.size()){
            return childs.get(index);
        }
        return null;
    }
}
